package com.example.demo.controller;


import com.example.demo.pojo.Category;
import com.example.demo.pojo.Result;

import com.example.demo.pojo.request.CategoryRequest;

import com.example.demo.service.ICategoryService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryControl {
    @Autowired
    private ICategoryService iCategoryService;
    //查询全部
    @GetMapping("/list")
    public Result list(){
        List<Category> select = iCategoryService.list();
        return Result.success(select);
    }

    //分页查询
    @GetMapping("/page")
    public Result page(CategoryRequest categoryRequest){
        Object page = iCategoryService.page(categoryRequest);
        return Result.success(page);
    }

    //添加数据
    @PostMapping("/add")
    public Result add(@RequestBody Category category){
        iCategoryService.save(category);
        return Result.success();
    }

    //根据id编辑回写
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Category category = iCategoryService.getUser(id);
        return Result.success(category);
    }

    //根据id编辑
    @PutMapping("/update")
    public Result update(@RequestBody Category category){
        iCategoryService.getIDUser(category);
        return Result.success();
    }

    //根据id删除
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable Integer id){
        iCategoryService.delete(id);
        return Result.success();
    }

}
